using System;
using System.Collections.Generic;
using UnityEngine;
using Script.My_Gameplay_Ability_System.Attributes.Components;

namespace Script.My_Gameplay_Ability_System.Attributes.Base
{
    /// <summary>
    /// 自定义事件参数类，用于在属性值修改前传递数据
    /// </summary>
    public class PreAttributeChangeEventArgs : EventArgs
    {
        // AttributeSystemComponent attributeSystem { get; set; }
        /// <summary>
        /// 变化的值
        /// </summary>
        float value { get; set; }
    }
    
    public class AttributeBase : MonoBehaviour
    {
        public event EventHandler PreAttributeChange;
        
        /// <summary>
        /// 触发属性值修改前的事件，用于在属性值修改前传递数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnPreAttributeChange(object sender, PreAttributeChangeEventArgs e)
        {
            EventHandler handler = PreAttributeChange;
            PreAttributeChange?.Invoke(this, e);
        }
        
        /// <summary>
        /// 计算属性的初始值，默认直接返回输入值（子类可重写）
        /// </summary>
        /// <param name="attributeValue">依赖属性值</param>
        /// <param name="otherAttributeValues">所有基础属性值</param>
        /// <returns>当前属性值</returns>
        public virtual AttributeValue CalculateInitialValue(AttributeValue attributeValue, List<AttributeValue> otherAttributeValues)
        {
            return attributeValue;
        }
        
        /// <summary>
        /// 计算属性修正后的值（子类可重写计算逻辑）
        /// </summary>
        /// <param name="attributeValue">基础属性值</param>
        /// <param name="otherAttributeValues">依赖属性值</param>
        /// <returns></returns>
        public virtual AttributeValue CalculateCurrentAttributeValue(AttributeValue attributeValue, List<AttributeValue> otherAttributeValues)
        {
            // 默认公式：(基础值 + 加法修正) * (乘法修正 + 1)
            attributeValue.CurrentValue = (attributeValue.BaseValue + attributeValue.Modifier.Add) * (attributeValue.Modifier.Multiply + 1);
        
            // 如果存在覆盖值，直接使用
            if (attributeValue.Modifier.Override != 0)
            {
                attributeValue.CurrentValue = attributeValue.Modifier.Override;
            }
            return attributeValue;
        }
    }
    
}
